package com.otitan.forest.right.realm;

import com.otitan.forest.right.constant.StaticConstant;
import com.otitan.forest.right.entity.SysRoleEntity;
import com.otitan.forest.right.entity.SysUserEntity;
import com.otitan.forest.right.service.SysRoleService;
import com.otitan.forest.right.service.SysUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 当前类作用描述
 * @Author: xiazw
 * @CreateDate: 2020/8/4 14:06
 */
public class MyShiroRealm extends AuthorizingRealm {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysRoleService sysRoleService;
    public static Map<String, SysUserEntity> qqLoginInfoMap = new HashMap<>();

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        SysUserEntity userInfo = (SysUserEntity) principals.getPrimaryPrincipal();
        List<SysRoleEntity> sysRoleEntities =sysRoleService.findRoleByUserId(userInfo.getId());
        for(SysRoleEntity role :sysRoleEntities){
            authorizationInfo.addRole(role.getRole());
        }
     /*   for (SysRoleEntity role : userInfo.getRoleList()) {
            authorizationInfo.addRole(role.getName());
           *//* for (Permission p : role.getPermissions()) {
                authorizationInfo.addStringPermission(p.getPermission());
            }*//*
        }*/
        return authorizationInfo;
    }

    /*主要是用来进行身份认证的，也就是说验证用户输入的账号和密码是否正确。*/
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)
            throws AuthenticationException {
        //设置登录过期时间，永不过期
        SecurityUtils.getSubject().getSession().setTimeout(-1000L);
        //获取用户的输入的账号.
        String username = (String) token.getPrincipal();
        //String password = new String((char[]) token.getCredentials());
        SimpleAuthenticationInfo authenticationInfo = null;

        SysUserEntity userInfo = sysUserService.findByUserName(username);

        if (userInfo == null) {
            return null;
        }
        ByteSource byteSourceSalt = ByteSource.Util.bytes(StaticConstant.SALT);
        authenticationInfo = new SimpleAuthenticationInfo(
                userInfo,
                userInfo.getPassword(),
                byteSourceSalt,
                getName()
        );
        return authenticationInfo;
    }
}
